package cn.backpackerxl.easyJDBC.util;


import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * @author: backpackerxl
 * @create: 2022/9/22
 * @filename: BasicUtil
 **/
public final class BasicUtil {

    private BasicUtil() {
    }

    /**
     * 格式化时间
     *
     * @param obj   时间对象
     * @param style 格式化格式
     * @return 时间
     */
    public static Object formatTime(Object obj, String style) {
        Date date = (Date) obj;
        return style.equals("#") ? date.getTime() : new SimpleDateFormat(style).format(date);
    }

    /**
     * 判断是否为自定义对象
     *
     * @param typeName 类型名
     * @return 返回是否为基础数据类型
     */
    public static boolean isMyObj(String typeName) {
        if (isType(typeName)) return false;
        return !typeName.startsWith("java.lang") && !typeName.startsWith("java.util");
    }

    public static boolean isType(String typeName) {
        return typeName.equals(Typeof.$INT.value()) || typeName.equals(Typeof.$LONG.value()) || typeName.equals(Typeof.$BYTE.value()) || typeName.equals(Typeof.$SHORT.value()) || typeName.equals(Typeof.$FLOAT.value()) || typeName.equals(Typeof.$DOUBLE.value()) || typeName.equals(Typeof.$BOOLEAN.value()) || typeName.equals(Typeof.CHAR.value());
    }

    /**
     * 判断是否是数组
     *
     * @param obj 判断值类型是否为数组
     * @return 是否为数组
     */
    public static boolean isArray(Object obj) {
        return obj instanceof int[] || obj instanceof Object[] || obj instanceof long[] || obj instanceof byte[] || obj instanceof char[] || obj instanceof boolean[] || obj instanceof short[] || obj instanceof double[] || obj instanceof float[];
    }

    public static boolean isArray(Field field) {
        return field.getType().getTypeName().contains("[]");
    }

    public static boolean isBasicArray(Field field) {
        return field.getType().getTypeName().contains("[]") && !field.getType().getTypeName().contains("java.lang") && !field.getType().getTypeName().contains("java.util");
    }

    public static String typeName(Field field) {
        return field.getType().getTypeName().replace("[]", "");
    }

    public static String getMethodName(String typeName) {
        Typeof typeof = MatchType.get(typeName);
        switch (Objects.requireNonNull(typeof)) {
            case STRING:
                return "getStrValue";
            case LONG:
                return "getLongValue";
            case SHORT:
                return "getShortValue";
            case BYTE:
                return "getByteValue";
            case BOOLEAN:
                return "getBoolValue";
            case DOUBLE:
                return "getDouValue";
            case INT:
                return "getIntValue";
            case FLOAT:
                return "getFloatValue";
            case OBJECT:
                return "getObjValue";
            default:
                throw new RuntimeException("parse Map or List error.");
        }
    }

}
